<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/2/6
 * Time: 16:37
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class SupplierOrdersInfoModel
{
    const TABLE = 'supplier_orders_info';

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $data
     * @return
     */
    public function lists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('material as m','m.id','=',self::TABLE.'.materialId')
            ->leftJoin('material_spec as ms','ms.id','=',self::TABLE.'.specId')
            ->leftJoin('unit as u','u.id','=',self::TABLE.'.unitId')
            ->where(function($query) use ($data){
                $query->where('orderId',$data['orderId']);
                if (!empty($data['month'])){
                    $startTime = (new \DateTime($data['month']))->format('Y-m-01 00:00:00');
                    $endTime = (new \DateTime($data['month']))->format('Y-m-t 23:59:59');
                    $query->whereBetween('deliveryTime',[$startTime,$endTime]);
                }
            })
            ->select(self::TABLE.'.*','u.name as unitName','m.name as materialName','ms.spec')
            ->get()->toArray();
    }

    public function info(array $param){
        $result = DB::table(self::TABLE)->where($param)->first();
        return empty($result)?[]:get_object_vars($result);
    }

    public function delete(array $param)
    {
        DB::beginTransaction();
        try{
            DB::table(self::TABLE)->where(['id'=>$param['id']])->delete();
            $sum = DB::table(self::TABLE)->where(['orderId'=>$param['orderId']])->sum('totalPrice');
            (new SupplierOrdersModel())->update(['id'=>$param['orderId']],['totalPrice'=>$sum]);
            DB::commit();
        }catch(\Exception $e){
            DB::rollback();
        }

    }

    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    public function generateCode($materialId)
    {
        $info = (new MaterialModel())->info(['id'=>$materialId]);
        $code = '';
        $categoryInfo = (new MaterialCategoryModel())->info(['id'=>$info['categoryId']]);
        switch ($info['categoryId']){
            case 3:
                $code.='A'.$categoryInfo['code'].$info['code'].date('Y');
                break;
            case 4:
                $code.='B'.$categoryInfo['code'].$info['code'].date('Y');
                break;
            default:
                $code.='AH'.'000'.date('Y');
                break;
        }
        $lastMaterialsn = DB::table(self::TABLE)
            ->where('materialsn','like',$code.'%')
            ->orderBy('materialsn','desc')
            ->first();
        $lastMaterialsn = empty($lastMaterialsn)?[]:get_object_vars($lastMaterialsn);
        $materialsn = empty($lastMaterialsn['materialsn'])?0:intval(substr($lastMaterialsn['materialsn'],9));
        $increment = sprintf("%06s",($materialsn+1));
        return $code.$increment;
    }
}